/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.daoimplementation;

import cz.muni.fi.pa036is.daointerface.AdmissionDAO;
import cz.muni.fi.pa036is.entity.Admission;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Kuba
 */
@Repository
public class AdmissionDAOImpl implements AdmissionDAO {

    private Connection con = null;
    private Statement st = null;
    private ResultSet rs = null;

    public AdmissionDAOImpl() throws ClassNotFoundException {



        String url = "jdbc:postgresql://localhost/PA036";
        String user = "postgres";
        String password = "test";

        try {
												Class.forName("org.postgresql.Driver");
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void addAdmission(Admission admission) {
        if (admission == null) {
            throw new IllegalArgumentException("Admission can not be null");
        }
        try {

            String insert = "INSERT INTO admission"
                    + "(person_id, season_id, date) VALUES"
                    + "(?,?,?)";
            PreparedStatement preparedStatement = con.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS);


            preparedStatement.setLong(1, admission.getPersonId());
            preparedStatement.setLong(2, admission.getSeasonId());
            preparedStatement.setTimestamp(3, admission.getDate());

            preparedStatement.executeUpdate();
												
			ResultSet rs = preparedStatement.getGeneratedKeys();
			rs.next();
			Long admissionId = rs.getLong(1);
			admission.setId(admissionId);

        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void editAdmission(Admission admission) {
        if (admission == null) {
            throw new IllegalArgumentException("Admission can not be null");
        }
        try {

            String update = "UPDATE admission SET date= ? WHERE person_id= ? AND season_id = ?";

            PreparedStatement preparedStatement = con.prepareStatement(update);

            preparedStatement.setTimestamp(1, admission.getDate());
            preparedStatement.setLong(2, admission.getPersonId());
            preparedStatement.setLong(3, admission.getSeasonId());
            preparedStatement.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void deleteAdmission(Admission admission) {
        if (admission == null) {
            throw new IllegalArgumentException("Admission can not be null");
        }

        try {

            String delete = "DELETE FROM admission WHERE person_id= ? and season_id= ?";

            PreparedStatement preparedStatement = con.prepareStatement(delete);

            preparedStatement.setLong(1, admission.getPersonId());
            preparedStatement.setLong(2, admission.getSeasonId());

            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public List<Admission> findAdmissionsForPerson(Long personId) {
        if (personId == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        List<Admission> admissionList = new ArrayList<Admission>();
        try {

            String find = "SELECT * FROM admission WHERE person_id=?";

            PreparedStatement preparedStatement = con.prepareStatement(find);

            preparedStatement.setLong(1, personId);

            rs = preparedStatement.executeQuery();

            while (rs.next()) {
                Admission admission = new Admission();
                admission.setPersonId(rs.getLong(1));
                admission.setSeasonId(rs.getLong(2));
                admission.setDate(rs.getTimestamp(3));
                admissionList.add(admission);

            }

        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return admissionList;
    }

    @Override
    public List<Admission> findAdmissionsForSeason(Long seasonId) {
        if (seasonId == null) {
            throw new IllegalArgumentException("ID can not be null");
        }

        List<Admission> admissionList = new ArrayList<Admission>();

        try {

            String find = "SELECT * FROM admission WHERE season_id=?";

            PreparedStatement preparedStatement = con.prepareStatement(find);

            preparedStatement.setLong(1, seasonId);

            rs = preparedStatement.executeQuery();

            while (rs.next()) {
                Admission admission = new Admission();
                admission.setPersonId(rs.getLong(1));
                admission.setSeasonId(rs.getLong(2));
                admission.setDate(rs.getTimestamp(3));
                admissionList.add(admission);

            }

        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return admissionList;
    }

    @Override
    public List<Admission> getAllAdmissions() {
        List<Admission> admissionList = new ArrayList<Admission>();

        try {

            String getAll = "SELECT * FROM admission";

            PreparedStatement preparedStatement = con.prepareStatement(getAll);

            rs = preparedStatement.executeQuery();

            while (rs.next()) {
                Admission admission = new Admission();
                admission.setPersonId(rs.getLong(1));
                admission.setSeasonId(rs.getLong(2));
                admission.setDate(rs.getTimestamp(3));
                admissionList.add(admission);

            }
        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return admissionList;
    }

    @Override
    public Admission findAdmission(Long personId, Long seasonId) {
        if (personId == null) {
            throw new IllegalArgumentException("ID can not be null");
        }

        Admission admission = new Admission();

        try {

            String find = "SELECT * FROM admission WHERE person_id=? AND season_id=?";

            PreparedStatement preparedStatement = con.prepareStatement(find);

            preparedStatement.setLong(1, personId);
			preparedStatement.setLong(2, seasonId);

            rs = preparedStatement.executeQuery();

            rs.next();
            admission.setPersonId(rs.getLong(1));
            admission.setSeasonId(rs.getLong(2));
            admission.setDate(rs.getTimestamp(3));

        } catch (SQLException ex) {
            Logger.getLogger(AdmissionDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return admission;
    }
}
